package com.lw.extraceim.loader;

import android.content.Context;

import androidx.annotation.NonNull;

import com.lw.extraceim.app.AppConstants;
import com.lw.extraceim.loader.base.BaseLoader;
import com.lw.extraceim.model.UserInfo;
import com.lw.extraceim.ui.activity.MainActivity;
import com.lw.extraceim.util.JsonUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户加载类
 */
public class UserLoader extends BaseLoader {
    public UserLoader(Context context) {
        super(context);
    }


    /**
     * 用户登录
     *
     * @param phone
     * @param password
     * @param callback
     */
    public void login(String phone, String password, @NonNull Callback callback) {
        String url = AppConstants.USER_SERVICE + "/login";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("phone", phone);
            jsonObject.put("password", password);
            execute(url, jsonObject.toString(), callback);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户注册
     *
     * @param userInfo
     * @param callback
     */
    public void register(UserInfo userInfo, @NonNull Callback callback) {
        String url = AppConstants.USER_SERVICE + "/add";
        String userJson = JsonUtils.toJson(userInfo);
        execute(url, userJson, callback);
    }

    /**
     * 退出登录
     *
     * @param userId
     * @param callback
     */
    public void logout(Integer userId, @NonNull Callback callback) {
        String url = AppConstants.USER_SERVICE + "/logout/" + userId;
        execute(url, AppConstants.GET, null, callback);
    }

    /**
     * 修改用户信息，但不修改用户密码
     *
     * @param userInfo
     * @param callback
     */
    public void update(UserInfo userInfo, @NonNull Callback callback) {
        String url = AppConstants.USER_SERVICE + "/update";
        userInfo.setPassword(null);
        String userJson = JsonUtils.toJson(userInfo);
        execute(url, userJson, callback);
    }

    /**
     * 忘记密码
     *
     * @param phone
     * @param password
     * @param callback
     */
    public void forgetPassword(String phone, String password, @NonNull Callback callback) {
        String url = AppConstants.USER_SERVICE + "/forget_password";
        Map<String, String> form = new HashMap<>();
        form.put("phone", phone);
        form.put("password", password);
        execute(url, AppConstants.POST, form, callback);
    }

    /**
     * 修改用户密码
     * @param phone
     * @param oldPassword 原密码
     * @param newPassword 新密码
     * @param callback
     */
    public void updatePassword(String phone, String oldPassword, String newPassword, @NonNull Callback callback) {
        String url = AppConstants.USER_SERVICE + "/forget_password";
        Map<String, String> form = new HashMap<>();
        form.put("phone", phone);
        form.put("oldPassword", oldPassword);
        form.put("newPassword",newPassword);
        execute(url, AppConstants.POST, form, callback);
    }



}
